package jEDF.Hypnogram;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

import jEDF.EDF.*;
import jEDF.JEDF.*;
import projetotcc2.ProjetoTCC2View;

/**
 *
 * <p>Titre : jEDF</p>
 * <p>Description : Java European Data Format Viewer and Analyser</p>
 * <p>Author : Nizar Kerkeni</p>
 * <p>Copyright : Copyright (c) 2003-2006</p>
 * <p>Version 2.0</p>
 */
public class HypnogramToTextDialog extends JDialog {

    JPanel panel1 = new JPanel();
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel2 = new JPanel();
    JButton jbOk = new JButton();
    JButton jbCancel = new JButton();
    Box box1 = Box.createVerticalBox();
    JLabel jLabel1 = new JLabel();
    JTextField jTextFileName = new JTextField();
    Component component1 = Box.createGlue();
    Box box2 = Box.createHorizontalBox();
    JButton jbBrowse = new JButton();
    Component component2 = Box.createGlue();
    Component component3 = Box.createGlue();
    private JEDFHypnogram hypnogram;

    /**
     * Constructor
     * @param mainwindow JEDFMainWindow
     * @param hypnogram JEDFHypnogram
     */
    public HypnogramToTextDialog(ProjetoTCC2View mainwindow, JEDFHypnogram hypnogram) {
        super(mainwindow.getFrame(), true);
        try {
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            jbInit();
            pack();
            setLocationRelativeTo(null);
            this.hypnogram = hypnogram;
            EDFFile edfFile = mainwindow.getEdfFile();
            if (edfFile == null) {
                return;
            }
            jTextFileName.setText(EDFFileFilter.changeFileExtension(edfFile.getFile(), "-hyp.txt"));
            setTitle(JEDFMainWindow.getLocalizedString("Export Hypnogram To Text"));
            setVisible(true);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        panel1.setLayout(borderLayout1);
        jbOk.setText(JEDFMainWindow.getLocalizedString("OK"));
        jbOk.addActionListener(new HypnogramToTextDialog_jbOk_actionAdapter(this));
        jbCancel.setText(JEDFMainWindow.getLocalizedString("Cancel"));
        jbCancel.addActionListener(new HypnogramToTextDialog_jbCancel_actionAdapter(this));
        jLabel1.setText(JEDFMainWindow.getLocalizedString("Output file"));
        jTextFileName.setMaximumSize(new Dimension(250, 19));
        jTextFileName.setPreferredSize(new Dimension(250, 19));
        jbBrowse.setSelectedIcon(null);
        jbBrowse.setText(JEDFMainWindow.getLocalizedString("Browse"));
        jbBrowse.addActionListener(new HypnogramToTextDialog_jbBrowse_actionAdapter(this));
        getContentPane().add(panel1);
        panel1.add(box1, java.awt.BorderLayout.NORTH);
        box1.add(box2);
        box2.add(jLabel1);
        box2.add(component3);
        box2.add(jTextFileName);
        box2.add(component2);
        box2.add(jbBrowse);
        box1.add(jPanel2);
        jPanel2.add(jbOk, null);
        jPanel2.add(component1);
        jPanel2.add(jbCancel, null);
    }

    public void jbCancel_actionPerformed(ActionEvent e) {
        dispose();
    }

    public void jbOk_actionPerformed(ActionEvent e) {
        File file = new File(jTextFileName.getText());
        if (file.exists()) {
            int result = JOptionPane.showConfirmDialog(this,
                    JEDFMainWindow.getLocalizedString(
                    "The specified file already exists.\nAre you sure you want to replace its content ?"),
                    JEDFMainWindow.getLocalizedString("Warning"),
                    JOptionPane.YES_NO_OPTION);
            if (result != JOptionPane.YES_OPTION) {
                return;
            }
            file.delete();
        }
        hypnogram.saveToTextFile(file, this);
    }

    public void jbBrowse_actionPerformed(ActionEvent e) {
        File file = new File(jTextFileName.getText());
        JFileChooser chooser = new JFileChooser();
        chooser.setSelectedFile(file);

        String[] extensions = {"txt"};

        EDFFileFilter edfFileFilter = new EDFFileFilter(extensions,
                JEDFMainWindow.getLocalizedString("Text file"));

        chooser.setFileFilter(edfFileFilter);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            edfFileFilter.changeFileExtension(chooser.getSelectedFile(),
                    "");
            String chosenFile = chooser.getSelectedFile().getAbsolutePath();

            if (!chosenFile.endsWith(".txt")) {
                chosenFile += ".txt";
            }

            jTextFileName.setText(chosenFile);
        }
    }
}

class HypnogramToTextDialog_jbOk_actionAdapter implements ActionListener {

    private HypnogramToTextDialog adaptee;

    HypnogramToTextDialog_jbOk_actionAdapter(HypnogramToTextDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jbOk_actionPerformed(e);
    }
}

class HypnogramToTextDialog_jbBrowse_actionAdapter implements ActionListener {

    private HypnogramToTextDialog adaptee;

    HypnogramToTextDialog_jbBrowse_actionAdapter(HypnogramToTextDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jbBrowse_actionPerformed(e);
    }
}

class HypnogramToTextDialog_jbCancel_actionAdapter implements ActionListener {

    private HypnogramToTextDialog adaptee;

    HypnogramToTextDialog_jbCancel_actionAdapter(HypnogramToTextDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jbCancel_actionPerformed(e);
    }
}
